const express = require("express");
const userList = require("../data/userData.json");
const roleList = require("../data/roleData.json");
const tokenList = require("../utils/token.json");
// 用户管理
const userControl = express.Router();

// 用户列表
userControl.get("/", function (req, res) {
  //   console.log(req.query);
  const filterUserList = userList.filter(
    (item) => item.username.indexOf(req.query.searchName) !== -1
  );

  const current = req.query.page;
  const size = req.query.limit;
  const start = (current - 1) * size;
  const end = current * size;
  // console.log(start, end);
  const activeUserList = filterUserList.filter((m) => !m.del);
  const resUserList = activeUserList.slice(start, end);
  return res.send({
    code: 200,
    message: "成功",
    data: {
      records: resUserList,
      total: activeUserList.length,
      size,
      current,
      orders: [],
      optimizeCountSql: true,
      hitCount: false,
      countId: null,
      maxLimit: null,
      searchCount: true,
      pages: current,
    },
    ok: true,
  });
});
// 新增用户
userControl.post("/save", function (req, res) {
  // console.log(req.body);
  const user = userList.find((m) => !m.del && m.username === req.body.username);
  if (user) {
    return res.send({
      code: 201,
      message: "用户名已存在",
      data: {},
      ok: true,
    });
  }
  userList.unshift({
    id: userList[0].id + 1,
    createTime: new Date().toLocaleString(),
    updateTime: undefined,
    username: req.body.username,
    password: req.body.password,
    name: req.body.name,
    phone: undefined,
    roleName: "普通用户",
  });
  return res.send({
    code: 200,
    message: "成功",
    data: {},
    ok: true,
  });
});
//修改用户
userControl.put("/update", function (req, res) {
  console.log(req.body);
  const user = userList.find((m) => !m.del && m.id === req.body.id);
  if (!user) {
    return res.send({
      code: 201,
      message: "未找到该用户",
      data: {},
      ok: true,
    });
  }
  user.updateTime = new Date().toLocaleString();
  user.username = req.body.username;
  user.name = req.body.name;
  // 如果是修改当前账户的信息 将token 废弃
  // if (req.body.id === req._id) {
  //   const token = req.headers.authorization.replace("Bearer ", "");
  //   tokenList.push(token);
  // }
  return res.send({
    code: 200,
    message: "成功",
    data: {},
    ok: true,
  });
});
// 删除用户
userControl.post("/remove", function (req, res) {
  // console.log(req.body);
  userList.forEach((item) => {
    if (req.body.userIdList.includes(item.id)) {
      item.del = true;
    }
  });
  return res.send({
    code: 200,
    message: "成功",
    data: {},
    ok: true,
  });
});

// 获取用户的角色信息
userControl.get("/roleList", function (req, res) {
  // console.log(req.query.id);
  const userRole = userList.find((item) => item.id == req.query.id);
  if (!userRole) {
    return res.send({
      code: 201,
      message: "该用户不存在",
      ok: true,
    });
  }
  const assignRoles = roleList.filter((item) =>
    userRole.roleName.includes(item.roleName)
  );
  return res.send({
    code: 200,
    message: "成功",
    data: {
      allRoleList: roleList,
      assignRoles,
    },
    ok: true,
  });
});
// 更新用户角色列表
userControl.post("/setRole", function (req, res) {
  // console.log(req.body);
  const newRoles = roleList.map((item) => {
    if (req.body.roleIdList.includes(item.id)) {
      return item.roleName;
    }
  });
  userList.forEach((item) => {
    if (item.id === req.body.userId) {
      item.roleName = newRoles;
    }
  });

  return res.send({
    code: 200,
    message: "成功",
    data: {},
    ok: true,
  });
});

module.exports = userControl;
